<template>
    <el-drawer title="文件选择器（数据来源：资源管理）" :visible.sync="visible" :direction="direction" :modal="false"
        :before-close="closeDrawer">
        <div class="groupSelector">
            <el-form-item>
                <el-select v-model="thisGroup" placeholder="请选择分组" filterable @change="changeGroup">
                    <el-option v-for="i in groups" :key="i.uuid" :label="i.groupName" :value="i.uuid"></el-option>
                </el-select>
            </el-form-item>
        </div>
        <div class="image upload-image" @click="uploadImage">
            <i class="el-icon-plus"></i>
            <input id="uploadImage" type="file" @change="fileChange">
        </div>
        <div class="image" v-for="file in files" :key="file.uuid" @click="changeFile(file.resource_url)">
            {{ file.resource_name + '.' + file.resource_type }}
        </div>
        
    </el-drawer>
</template>
  
<script>
export default {
    props: ["visible", "value"],
    data () {
        return {
            // 可用图标，后期使用字典配置代替
            files: [],
            direction: "rtl",
            thisGroup: undefined,
            groups: [],
            deftValue: this.value,
        }
    },
    mounted () {
        this.getGroupList()
        this.getFiles(this.thisGroup)
    },
    methods: {
        changeGroup () {
            this.getFiles(this.thisGroup)
        },
        // 获取分组列表
        getGroupList () {
            this.groupLoading = true
            this.groups = [
                {
                    uuid: undefined,
                    groupName: "全部资源"
                }
            ]
            this.$api.resource.getGroupList().then(res => {
                if (res.code == 200 && res.data != null) {
                    res.data.forEach(e => {
                        this.groups.push(e)
                    })
                }
                this.groupLoading = false
            })
        },
        fileChange (event) {
            let files = event.target.files || event.dataTransfer.files
            if (files && files.length > 0) {
                let file = files[0]
                const formData = new FormData()
                formData.append("file", file)
                formData.append("groupName", this.thisGroup)
                this.$api.resource.uploadFile(formData).then(res => {
                    if (res.code == 200) {
                        this.$message.success("上传成功!")
                        this.getFiles(this.thisGroup)
                    } else {
                        this.$message.error(res.msg)
                    }
                })
            }
        },
        uploadImage () {
            let fileInput = document.querySelector("#uploadImage")
            fileInput.click()
        },
        getFiles (groupName) {
            let vo = {
                objectCode: "sys_resource",
                page: {
                    page: false,
                },
                enableDataIsolation: false,
                fields: [],
                sortFields: "create_time",
                sortType: "DESC",
            }
            if(groupName){
                vo.fields.push({
                        fieldCode: "group_name",
                        operator: "=",
                        value: groupName,
                    })
            }
            this.$api.baseData.list(vo).then(res => {
                if (res.code == 200) {
                    console.log(res);
                    this.files = res.data.list
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
        changeFile (resourceUrl) {
            console.log(resourceUrl);
            this.$emit('update:value', resourceUrl)
        },
        closeDrawer () {
            this.$emit('update:visible', false)
        },
    },
}
</script>
  
<style lang="less" scoped>
.groupSelector {
    padding: 10px;
}

.image {
    width: calc(100% - 22px);
    height: auto;
    margin: 10px;
    border: 1px dashed #d9d9d9;
    cursor: pointer;
    border-radius: 6px;
}

.image:hover {
    border-color: #409eff;
}

.upload-image {
    font-size: 28px;
    color: #8c939d;
    line-height: 62px;
    text-align: center;

    input {
        display: none;
    }
}
</style>